<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>05.Vue中结合Axios完成天气查询案例</title>
</head>
<body>
<div id="app">


    <input type="text"  v-model="name" @keyup.delete="shows" @keyup.enter="searchCity"/> <input type="button" value="搜索" @click="searchCity"/> <br>

    <span v-for="city in hotCitys">
        <a href="" @click.prevent="searchCitys(city)">{{ city }}</a> &nbsp;
    </span>

    <hr>

    <span v-show="isShow">{{ name }},今天的天气是: {{ message }} </span>

</div>
<!--引入vue-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--引入axios-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            name:"",
            hotCitys:["北京","上海","广州","深圳"],
            message:"",
            isShow:false,
        },
        methods: {
            searchCity(){
                //获取输入的城市信息
                let _this = this;
                //发送axios请求
                axios.get("http://localhost:8989/city/find?name="+this.name).then(function (response) {
                    console.log(response.data.message);
                    _this.message = response.data.message;
                    _this.isShow = true;
                }).catch(function (err) {
                    console.log(err);
                });
            },
            searchCitys(name){
                this.name = name;
                this.searchCity();//函数中调用函数
            },
            shows(){
                this.isShow = false;
            }
        }
    });
</script>
</body>
</html>